package com.core.sgip.common.body.command;

import java.util.Arrays;

import org.apache.log4j.Logger;

import com.core.sgip.common.SGIPUtils;
import com.core.sgip.common.body.SGIPCommand;

public class BindResp extends SGIPCommand {

	private static Logger logger = Logger.getLogger(BindResp.class);

	private static final int COMMAND_LENGTH = 9;

	private int result;

	private String reservestr;

	private byte[] resultByte = new byte[1];

	private byte[] reserve = new byte[8];

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String getReservestr() {
		return reservestr;
	}

	public void setReservestr(String reservestr) {
		this.reservestr = reservestr;
	}

	public BindResp() {
		super();
		this.commandLength = COMMAND_LENGTH;
	}

	@Override
	public byte[] getByteData() {
		this.resultByte[0] = SGIPUtils.convertInt2Byte(this.result);

		byte[] source = this.reservestr.getBytes();
		int length = source.length;
		SGIPUtils.copyBytes(source, this.reserve, 1, length, 1);
		return SGIPUtils.mergeBytes(this.resultByte, this.reserve);
	}

	@Override
	public void initPropertiesByBytes(byte[] source) {
		if (source != null && source.length >= COMMAND_LENGTH) {
			SGIPUtils.copyBytes(source, this.resultByte, 1, 1, 1);
			this.result = SGIPUtils.convertUnsignByte2Int(this.resultByte[0]);
			this.reservestr = new String(this.reserve);
		} else {
			logger.error("init bindResp properties from bytes error: the bytes not enough");
		}
	}

	@Override
	public String toString() {
		return "BindResp [result=" + result + ", reservestr=" + reservestr + ", resultByte="
				+ Arrays.toString(resultByte) + ", reserve=" + Arrays.toString(reserve) + "]";
	}

}
